package com.bbx.taxi.client.Service;

import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.bbx.api.gclient.GMsg;
import com.bbx.api.gclient.GMsgBroadcastReceiver;
import com.bbx.api.gclient.Observer;
import com.bbx.api.sdk.SDK;
import com.bbx.api.sdk.model.base.BaseModel;
import com.bbx.api.sdk.model.passanger.Token;
import com.bbx.api.sdk.model.passanger.Return.Position;
import com.bbx.api.sdk.model.passanger.Return.Jmsg.Jmsg;
import com.bbx.api.sdk.net.base.JsonBuild;
import com.bbx.api.util.LogUtils;
import com.bbx.api.util.SharedPreEncryptUtil;
import com.bbx.taxi.client.MyApplication;
import com.bbx.taxi.client.R;
import com.bbx.taxi.client.Bean.Value;
import com.bbx.taxi.client.Bean.Message.MyMesssage;
import com.bbx.taxi.client.Bean.Message.Online;
import com.bbx.taxi.client.DB.JmsgDB;
import com.bbx.taxi.client.Util.DateFormat;
import com.bbx.taxi.client.Util.WakeLockUtil;
import com.bbx.taxi.client.config.Config;

import android.app.ActivityManager;
import android.app.Notification;
import android.app.PendingIntent;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Binder;
import android.os.IBinder;

public class LoginService extends Service implements Observer {
	public static String ACTION="com.bbx.taxi.client.Service.LoginService";
	public static boolean SDKlogin=false;
	private static LoginService mInstance = null;
	private MyMesssage mMyMesssage;
	private Token token;
	private Online mOnline;
	/**判断是否执行onCreate*/
    private boolean isCreate=false;
	/**判断是否执行onCreate*/
    private boolean isStart=false;
	public static LoginService getInstance() {
		return mInstance;
	}

	private final IBinder binder = new MyBinder();

	public class MyBinder extends Binder {
		public LoginService getService() {
			return LoginService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return binder;
	}

	@Override
	public void onCreate() {
		super.onCreate();
		int ico = R.drawable.ic_launcher;
		// 通知栏
		CharSequence tishi =getString(R.string.app_name);
		long when = DateFormat.getTime(); // 获取当前时间
		Notification notification = new Notification(ico, tishi, when);
		startForeground(0, notification);
		mInstance = this;
		mOnline=new Online();
		// 为本类添加为可推送类
		SDK.GetSDK().addObs(this);
		if(mOnline!=null)
		mOnline.registerReceiver(this);
		isCreate=true;
	}
	@Override
	public void onStart(Intent intent, int startId) {
		WakeLockUtil.setWifiNeverSleep(this);
		super.onStart(intent, startId);
		isStart=true;
		// SDK登陆
		token = SharedPreEncryptUtil.getStringEncryptValue(this,
				SharedPreEncryptUtil.TokenAndUid, "");
		if(token!=null&&!SDKlogin){
			SDK.GetSDK().mClient.setLogpath(Value.SDK_PATH);
	    	SDK.ins.login(token.uid ,token.access_token);
//			if(!isCreate&&isStart){
//				mMyMesssage=MyMesssage.getInstance();
//				mMyMesssage.getOfflineMessage(token.uid, this);
//				
//			}
		}
		isCreate=false;
		isStart=false;

	}
    /**
     * 获取地理位置
     */
    public void getDriverLocation(String driver_id,String line_id){
    	LogUtils.e("", "开始获取地理位置" + driver_id);
    	JSONObject jsonObject = null;
		try {
			jsonObject = new JSONObject();
			jsonObject.put("command", 4);
			jsonObject.put("line_id", line_id);
			jsonObject.put("uid", driver_id);

		} catch (JSONException e) {
			e.printStackTrace();
		}
		// 获取地理位置
		SDK.ins.mClient.request(token.uid, mMyMesssage.sn_position, 30,
				jsonObject.toString());
    }
	@Override
	public void onDestroy() {
		SDKlogin=false;
		if (token!=null)
			SDK.ins.disconnect(token.uid);
		SDK.GetSDK().removeObs(this);
		if(mOnline!=null)
			mOnline.unregisterReceiver(this);
		super.onDestroy();
	}

	@Override
	public int onNotify(GMsg notify) {
		if (notify == null) {
			return 0;
		}
		mMyMesssage=MyMesssage.getInstance();
		mMyMesssage.onAllMessage(this, notify, token.uid, token.access_token);
		return 0;
	}

}
